<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li class="active"><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 手工测试">1. 手工测试</a></li><li><a href="#t12. assert断言">2. assert断言</a></li><li><a href="#t23. 测试框架">3. 测试框架</a></li><li><a href="#t34.开发模式">4.开发模式</a><ul><li><a href="#t44.1 瀑布模式">4.1 瀑布模式</a></li><li><a href="#t54.2 TDD">4.2 TDD</a></li><li><a href="#t64.3 TDD">4.3 TDD</a></li></ul></li><li><a href="#t75. 常用测试框架">5. 常用测试框架</a></li><li><a href="#t86. Jest">6. Jest</a><ul><li><a href="#t96.1 安装">6.1 安装</a></li><li><a href="#t106.2 编写">6.2 编写</a></li><li><a href="#t116.3 运行">6.3 运行</a></li><li><a href="#t126.4 编写测试用例">6.4 编写测试用例</a></li><li><a href="#t136.5 配置">6.5 配置</a><ul><li><a href="#t146.5.1 配置位置">6.5.1 配置位置</a></li><li><a href="#t156.5.2 配置项">6.5.2 配置项</a></li><li><a href="#t166.5.3 Matchers">6.5.3 Matchers</a></li></ul></li><li><a href="#t176.6 DOM测试">6.6 DOM测试</a></li><li><a href="#t186.7 测试选项卡">6.7 测试选项卡</a></li><li><a href="#t196.8 异步测试">6.8 异步测试</a><ul><li><a href="#t206.8.1 普通异步">6.8.1 普通异步</a></li><li><a href="#t216.8.2 ajax异步">6.8.2 ajax异步</a></li><li><a href="#t226.9 代码覆盖率">6.9 代码覆盖率</a></li></ul></li></ul></li><li><a href="#t237.附录">7.附录</a><ul><li><a href="#t247.1 glob">7.1 glob</a><ul><li><a href="#t257.2 glob规则">7.2 glob规则</a></li><li><a href="#t267.3 glob示例">7.3 glob示例</a></li></ul></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x624B;&#x5DE5;&#x6D4B;&#x8BD5;">1. &#x624B;&#x5DE5;&#x6D4B;&#x8BD5; <a href="#t01. &#x624B;&#x5DE5;&#x6D4B;&#x8BD5;"> # </a></h2>
<p>qs.js</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parse</span>(<span class="hljs-params">str</span>) </span>{
    <span class="hljs-keyword">let</span> arr = str.split(<span class="hljs-string">&apos;&amp;&apos;</span>);
    <span class="hljs-keyword">let</span> obj = {};
    arr.forEach(<span class="hljs-function">(<span class="hljs-params">item</span>) =&gt;</span> {
        <span class="hljs-keyword">let</span> [key, val] = item.split(<span class="hljs-string">&apos;=&apos;</span>);
        obj[key] = val;
    });
    <span class="hljs-keyword">return</span> obj;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">stringify</span>(<span class="hljs-params">obj</span>) </span>{
    <span class="hljs-keyword">let</span> arr = [];
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> key <span class="hljs-keyword">in</span> obj) {
        arr.push(key + <span class="hljs-string">&apos;=&apos;</span> + obj[key]);
    }
    <span class="hljs-keyword">return</span> arr.join(<span class="hljs-string">&apos;&amp;&apos;</span>);
}
exports.parse = parse;
exports.stringify = stringify;
</code></pre>
<h2 id="t12. assert&#x65AD;&#x8A00;">2. assert&#x65AD;&#x8A00; <a href="#t12. assert&#x65AD;&#x8A00;"> # </a></h2>
<ul>
<li>&#x65AD;&#x8A00;&#x662F;&#x8868;&#x8FBE;&#x8BBE;&#x8BA1;&#x4EBA;&#x5458;&#x5BF9;&#x4E8E;&#x7CFB;&#x7EDF;&#x5E94;&#x8FBE;&#x5230;&#x72B6;&#x6001;&#x7684;&#x4E00;&#x79CD;&#x9884;&#x671F;</li>
<li>&#x5404;&#x79CD;&#x8BED;&#x8A00;&#x90FD;&#x5185;&#x7F6E;&#x4E86;&#x65AD;&#x8A00;&#x7684;&#x63A5;&#x53E3;</li>
<li>&#x65AD;&#x8A00;&#x662F;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x7684;&#x6838;&#x5FC3;</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:left">&#x95EE;&#x9898;</th>
<th style="text-align:left">&#x89E3;&#x51B3;&#x65B9;&#x6848;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">&#x6C61;&#x67D3; &#x6E90;&#x4EE3;&#x7801;&#x91CC;&#x6DF7;&#x6742;&#x4E86;&#x5F88;&#x591A;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;</td>
<td style="text-align:left">&#x4ECE;&#x6E90;&#x4EE3;&#x7801;&#x4E2D;&#x62BD;&#x79BB;&#x51FA;&#x53BB;</td>
</tr>
<tr>
<td style="text-align:left">&#x96F6;&#x6563; &#x6742;&#x4E71;&#x65E0;&#x7AE0;&#xFF0C;&#x4E0D;&#x80FD;&#x5206;&#x7EC4;&#x548C;&#x5206;&#x7C7B;</td>
<td style="text-align:left">&#x6574;&#x4F53;&#x89C4;&#x5212; &#x548C;&#x8BBE;&#x8BA1;</td>
</tr>
<tr>
<td style="text-align:left">&#x6CA1;&#x6709;&#x6301;&#x4E45;&#x5316; &#x6CA1;&#x6709;&#x5B58;&#x50A8;</td>
<td style="text-align:left">&#x628A;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x5355;&#x72EC;&#x5B58;&#x653E;</td>
</tr>
<tr>
<td style="text-align:left">&#x624B;&#x52A8;&#x8DD1;&#x6D4B;&#x8BD5;&#x6BD4;&#x8F83;&#x9EBB;&#x70E6;</td>
<td style="text-align:left">&#x81EA;&#x52A8;&#x8FD0;&#x884C;&#x5E76;&#x663E;&#x793A;&#x7ED3;&#x679C;</td>
</tr>
</tbody>
</table>
<h2 id="t23. &#x6D4B;&#x8BD5;&#x6846;&#x67B6;">3. &#x6D4B;&#x8BD5;&#x6846;&#x67B6; <a href="#t23. &#x6D4B;&#x8BD5;&#x6846;&#x67B6;"> # </a></h2>
<ul>
<li>&#x901A;&#x8FC7;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5206;&#x79BB;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x548C;&#x6E90;&#x4EE3;&#x7801;</li>
<li>&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x8FD0;&#x884C;&#x6240;&#x6709;&#x7684;&#x7528;&#x4F8B;&#x5E76;&#x8F93;&#x51FA;&#x7ED3;&#x679C;</li>
<li>&#x6D4B;&#x8BD5;&#x6846;&#x67B6;&#x53EF;&#x80FD;&#x63D0;&#x9AD8;&#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x7684;&#x6548;&#x7387;</li>
</ul>
<h2 id="t34.&#x5F00;&#x53D1;&#x6A21;&#x5F0F;">4.&#x5F00;&#x53D1;&#x6A21;&#x5F0F; <a href="#t34.&#x5F00;&#x53D1;&#x6A21;&#x5F0F;"> # </a></h2>
<h3 id="t44.1 &#x7011;&#x5E03;&#x6A21;&#x5F0F;">4.1 &#x7011;&#x5E03;&#x6A21;&#x5F0F; <a href="#t44.1 &#x7011;&#x5E03;&#x6A21;&#x5F0F;"> # </a></h3>
<ol>
<li>&#x5206;&#x6790;&#x9700;&#x6C42;</li>
<li>&#x8C03;&#x8BD5;&#x5F00;&#x53D1;</li>
<li>&#x6D4B;&#x8BD5;&#x53D1;&#x5E03;</li>
</ol>
<h3 id="t54.2 TDD">4.2 TDD <a href="#t54.2 TDD"> # </a></h3>
<p><code>Test-Driven-Development</code> &#x6D4B;&#x8BD5;&#x9A71;&#x52A8;&#x5F00;&#x53D1;&#xFF0C;</p>
<ol>
<li>&#x5206;&#x6790;&#x9700;&#x6C42;</li>
<li>&#x4EFB;&#x52A1;&#x3001;&#x6A21;&#x5757;&#x5206;&#x89E3;</li>
<li>&#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;,&#x5C3D;&#x91CF;&#x7A77;&#x5C3D;&#x548C;&#x8003;&#x8651;&#x6240;&#x6709;&#x8FB9;&#x754C;</li>
<li>&#x5FEB;&#x901F;&#x6784;&#x5EFA;&#x4EE3;&#x7801;&#x8DD1;&#x901A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;</li>
<li>&#x91CD;&#x6784;&#x4EE3;&#x7801;</li>
<li>&#x529F;&#x80FD;&#x6D4B;&#x8BD5;&#x3001;&#x53D1;&#x5E03;</li>
</ol>
<h3 id="t64.3 TDD">4.3 TDD <a href="#t64.3 TDD"> # </a></h3>
<ul>
<li>&#x5728;TDD&#x7406;&#x5FF5;&#x4E2D;&#xFF0C;&#x5148;&#x6709;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x518D;&#x6709;&#x529F;&#x80FD;&#x903B;&#x8F91;&#x4EE3;&#x7801;</li>
<li>&#x5305;&#x62EC;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x548C;&#x65AD;&#x8A00;</li>
<li>&#x5206;&#x4E3A;&#x6A21;&#x5757;&#x6D4B;&#x8BD5;&#x548C;&#x5355;&#x5143;&#x6D4B;&#x8BD5;</li>
<li>&#x6709;&#x5176;&#x4F7F;&#x7528;&#x573A;&#x666F;&#xFF0C;&#x4E0D;&#x80FD;&#x6EE5;&#x7528;</li>
<li>&#x5728;&#x5B9E;&#x9645;&#x5F00;&#x53D1;&#x4E2D;&#x4E00;&#x822C;&#x4F1A;&#x4F7F;&#x7528;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;</li>
</ul>
<h2 id="t75. &#x5E38;&#x7528;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;">5. &#x5E38;&#x7528;&#x6D4B;&#x8BD5;&#x6846;&#x67B6; <a href="#t75. &#x5E38;&#x7528;&#x6D4B;&#x8BD5;&#x6846;&#x67B6;"> # </a></h2>
<ul>
<li><a href="https://github.com/qunitjs/qunit">qunit</a> jQuery</li>
<li><a href="https://github.com/mochajs/mocha">mocha</a> &#x652F;&#x6301;Node&amp;Browser express.js</li>
<li><a href="https://github.com/jasmine/jasmine">jasmine</a>&#x652F;&#x6301;Node&amp;Browser Vue.js</li>
<li><a href="https://github.com/karma-runner/karma">karma</a> A Test-Runner &#x5728;&#x4E0D;&#x540C;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x8DD1;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B; Angular </li>
<li><a href="https://github.com/facebook/jest">jest</a> React<ul>
<li>&#x96F6;&#x914D;&#x7F6E;</li>
<li>&#x5185;&#x7F6E;&#x4EE3;&#x7801;&#x8986;&#x76D6;&#x7387;</li>
<li>&#x5185;&#x7F6E;Mocks</li>
</ul>
</li>
</ul>
<h2 id="t86. Jest">6. Jest <a href="#t86. Jest"> # </a></h2>
<p><a href="https://facebook.github.io/jest/docs/en/expect.html">Jest&#x5B98;&#x65B9;&#x6587;&#x6863;</a></p>
<h3 id="t96.1 &#x5B89;&#x88C5;">6.1 &#x5B89;&#x88C5; <a href="#t96.1 &#x5B89;&#x88C5;"> # </a></h3>
<pre><code class="lang-js">cnpm i jest --save-dev 
cnpm i jest -g
</code></pre>
<h3 id="t106.2 &#x7F16;&#x5199;">6.2 &#x7F16;&#x5199; <a href="#t106.2 &#x7F16;&#x5199;"> # </a></h3>
<p>**test.js</p>
<h3 id="t116.3 &#x8FD0;&#x884C;">6.3 &#x8FD0;&#x884C; <a href="#t116.3 &#x8FD0;&#x884C;"> # </a></h3>
<pre><code class="lang-js">jest npm jest 
npm test
</code></pre>
<h3 id="t126.4 &#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;">6.4 &#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B; <a href="#t126.4 &#x7F16;&#x5199;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;"> # </a></h3>
<ul>
<li>Test Suits &#x6D4B;&#x8BD5;&#x5957;&#x4EF6;&#xFF0C;&#x6BCF;&#x4E2A;&#x6587;&#x4EF6;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x5957;&#x4EF6;</li>
<li>Test Group &#x5206;&#x7EC4; describe</li>
<li>Test Case &#x6D4B;&#x8BD5;&#x7528;&#x9014; test()</li>
<li>Assert &#x65AD;&#x8A00; expect()</li>
</ul>
<p>qs.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> { parse, stringify } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./qs&apos;</span>);
describe(<span class="hljs-string">&apos;parse&apos;</span>, () =&gt; {<span class="hljs-comment">//&#x5206;&#x7EC4;</span>
    test(<span class="hljs-string">&apos;one&apos;</span>, () =&gt; {<span class="hljs-comment">//&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;</span>
        expect(parse(<span class="hljs-string">&quot;name=zfpx&quot;</span>).name).toBe(<span class="hljs-string">&apos;zfpx&apos;</span>);
    });
    test(<span class="hljs-string">&apos;two&apos;</span>, () =&gt; {
        expect(parse(<span class="hljs-string">&quot;name=zfpx&amp;age=9&quot;</span>).age).toBe(<span class="hljs-string">&apos;9&apos;</span>);
    });
});


describe(<span class="hljs-string">&apos;stringify&apos;</span>, () =&gt; {
    test(<span class="hljs-string">&apos;one&apos;</span>, () =&gt; {
        expect(stringify({ <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span> })).toBe(<span class="hljs-string">&apos;name=zfpx&apos;</span>);
    });
    test(<span class="hljs-string">&apos;two&apos;</span>, () =&gt; {
        expect(stringify({ <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">9</span> })).toBe(<span class="hljs-string">&apos;name=zfpx&amp;age=9&apos;</span>);
    });
});
</code></pre>
<h3 id="t136.5 &#x914D;&#x7F6E;">6.5 &#x914D;&#x7F6E; <a href="#t136.5 &#x914D;&#x7F6E;"> # </a></h3>
<h4 id="t146.5.1 &#x914D;&#x7F6E;&#x4F4D;&#x7F6E;">6.5.1 &#x914D;&#x7F6E;&#x4F4D;&#x7F6E; <a href="#t146.5.1 &#x914D;&#x7F6E;&#x4F4D;&#x7F6E;"> # </a></h4>
<ul>
<li>package.json</li>
<li>jest.config.js</li>
<li>&#x547D;&#x4EE4;&#x884C;</li>
</ul>
<h4 id="t156.5.2 &#x914D;&#x7F6E;&#x9879;">6.5.2 &#x914D;&#x7F6E;&#x9879; <a href="#t156.5.2 &#x914D;&#x7F6E;&#x9879;"> # </a></h4>
<ul>
<li>testMatch glob&#x89C4;&#x5219;,&#x8BC6;&#x522B;&#x54EA;&#x4E9B;&#x6587;&#x4EF6;&#x4E2D;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;</li>
<li>testRegex &#x6587;&#x4EF6;&#x6B63;&#x5219;</li>
<li>testEnvironment &#x6D4B;&#x8BD5;&#x73AF;&#x5883; </li>
<li>rootDir &#x6839;&#x76EE;&#x5F55; </li>
<li>moduleFileExtensions &#x6A21;&#x5757;&#x6587;&#x4EF6;&#x6269;&#x5C55;&#x540D;</li>
</ul>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">testMatch</span>: [<span class="hljs-string">&apos;**/__tests__/**/*.js?(x)&apos;</span>, <span class="hljs-string">&apos;**/?(*.)(spec|test).js?(x)&apos;</span>],<span class="hljs-comment">//&#x8BBE;&#x7F6E;&#x8BC6;&#x522B;&#x54EA;&#x4E9B;&#x6587;&#x4EF6;&#x662F;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#xFF08;glob&#x5F62;&#x5F0F;&#xFF09;&#xFF0C;&#x4E0E;testRegex&#x4E92;&#x65A5;&#xFF0C;&#x4E0D;&#x80FD;&#x540C;&#x65F6;&#x5199;</span>
    testRegex: <span class="hljs-string">&apos;(/__tests__).*|(\\.|/)(test|spec))\\.jsx?$&apos;</span>,<span class="hljs-comment">//&#x8BBE;&#x7F6E;&#x8BC6;&#x522B;&#x54EA;&#x4E9B;&#x6587;&#x4EF6;&#x662F;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#xFF08;&#x6B63;&#x5219;&#x5F62;&#x5F0F;&#xFF09;&#xFF0C;&#x4E0E;testMatch&#x4E92;&#x65A5;&#xFF0C;&#x4E0D;&#x80FD;&#x540C;&#x65F6;&#x5199;</span>
    testEnvironment: <span class="hljs-string">&apos;jsdom&apos;</span>,<span class="hljs-comment">//&#x6D4B;&#x8BD5;&#x73AF;&#x5883;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x662F;&#xFF1A;jsdom&#xFF0C;&#x53EF;&#x4FEE;&#x6539;&#x4E3A;node</span>
    rootDir: <span class="hljs-string">&apos;&apos;</span>,<span class="hljs-comment">//&#x9ED8;&#x8BA4;&#x503C;&#xFF1A;&#x5F53;&#x524D;&#x76EE;&#x5F55;&#xFF0C;&#x4E00;&#x822C;&#x662F;package.json&#x6240;&#x5728;&#x7684;&#x76EE;&#x5F55;&#x3002;</span>
    moduleFileExtensions: [<span class="hljs-string">&apos;js&apos;</span>, <span class="hljs-string">&apos;json&apos;</span>, <span class="hljs-string">&apos;jsx&apos;</span>, <span class="hljs-string">&apos;node&apos;</span>]<span class="hljs-comment">//&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x7684;&#x7C7B;&#x578B;</span>
}
</code></pre>
<h4 id="t166.5.3 Matchers">6.5.3 Matchers <a href="#t166.5.3 Matchers"> # </a></h4>
<ul>
<li>&#x76F8;&#x7B49;&#x65AD;&#x8A00;<ul>
<li>toBe(value)&#xFF1A; &#x6BD4;&#x8F83;&#x6570;&#x5B57;&#x3001;&#x5B57;&#x7B26;&#x4E32;</li>
<li>toEqual(value)&#xFF1A; &#x6BD4;&#x8F83;&#x5BF9;&#x8C61;&#x3001;&#x6570;&#x7EC4;</li>
<li>toBeNull()</li>
<li>toBeUndefined()</li>
</ul>
</li>
<li>&#x5305;&#x542B;&#x65AD;&#x8A00;<ul>
<li>toHaveProperty(keyPath, value)&#xFF1A; &#x662F;&#x5426;&#x6709;&#x5BF9;&#x5E94;&#x7684;&#x5C5E;&#x6027;</li>
<li>toContain(item)&#xFF1A; &#x662F;&#x5426;&#x5305;&#x542B;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#xFF0C;&#x62EC;&#x53F7;&#x91CC;&#x5199;&#x4E0A;&#x6570;&#x7EC4;&#x3001;&#x5B57;&#x7B26;&#x4E32;</li>
<li>toMatch(regexpOrString)&#xFF1A; &#x62EC;&#x53F7;&#x91CC;&#x5199;&#x4E0A;&#x6B63;&#x5219;</li>
</ul>
</li>
<li>&#x903B;&#x8F91;&#x65AD;&#x8A00;,&#x5728;JavaScript&#x4E2D;&#xFF0C;&#x6709;&#x516D;&#x4E2A;falsy&#x503C;&#xFF1A;false&#xFF0C;0&#xFF0C;&apos;&apos;&#xFF0C;null&#xFF0C; undefined&#xFF0C;&#x548C;NaN&#x3002;&#x5176;&#x4ED6;&#x4E00;&#x5207;&#x90FD;&#x662F;Truthy&#x3002;<ul>
<li>toBeTruthy() </li>
<li>toBeFalsy()</li>
<li>oBeGreaterThan(number)&#xFF1A; &#x5927;&#x4E8E;</li>
<li>toBeLessThan(number)&#xFF1A; &#x5C0F;&#x4E8E;</li>
</ul>
</li>
<li>not &#x53D6;&#x53CD;</li>
</ul>
<pre><code class="lang-js">test(<span class="hljs-string">&apos;matchers&apos;</span>, () =&gt; {
    <span class="hljs-keyword">const</span> a = {
        <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;a&apos;</span>,
        <span class="hljs-attr">home</span>: {
            <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;beijing&apos;</span>
        }
    }
    <span class="hljs-keyword">const</span> b = {
        <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;a&apos;</span>,
        <span class="hljs-attr">home</span>: {
            <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;beijing&apos;</span>
        }
    }
    expect(a).toEqual(b)
    expect([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]).toEqual([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>])
    expect(<span class="hljs-literal">null</span>).toBeNull()

    expect([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]).toContain(<span class="hljs-number">1</span>)
    expect(b).toHaveProperty(<span class="hljs-string">&apos;home&apos;</span>)
    expect(<span class="hljs-string">&apos;abc&apos;</span>).toContain(<span class="hljs-string">&apos;b&apos;</span>)
    expect(<span class="hljs-string">&apos;abc&apos;</span>).toMatch(<span class="hljs-regexp">/^\w+$/</span>)
    expect(<span class="hljs-string">&apos;123&apos;</span>).not.toContain(<span class="hljs-string">&apos;4&apos;</span>)
})
</code></pre>
<h3 id="t176.6 DOM&#x6D4B;&#x8BD5;">6.6 DOM&#x6D4B;&#x8BD5; <a href="#t176.6 DOM&#x6D4B;&#x8BD5;"> # </a></h3>
<ul>
<li><a href="https://github.com/jsdom/jsdom">jsdom</a></li>
<li>Jest&#x80FD;&#x64CD;&#x4F5C;DOM&#x662F;&#x56E0;&#x4E3A;&#x5185;&#x7F6E;&#x4E86;JSDOM</li>
<li>JSDOM&#x662F;&#x5728;node&#x4E2D;&#x6A21;&#x62DF;&#x4E86;DOM&#x73AF;&#x5883; </li>
</ul>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">remove</span>(<span class="hljs-params">node</span>) </span>{
    node.parentNode.removeChild(node);
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">on</span>(<span class="hljs-params">node, type, handler</span>) </span>{
    node.addEventListener(type, handler);
}
exports.remove = remove;
exports.on = on;
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> { remove, on } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../src/dom&apos;</span>);
describe(<span class="hljs-string">&apos;dom&apos;</span>, () =&gt; {
    test(<span class="hljs-string">&apos;remove&apos;</span>, () =&gt; {
        <span class="hljs-built_in">document</span>.body.innerHTML = <span class="hljs-string">&apos;&lt;div id=&quot;container&quot;&gt;&lt;span id=&quot;hello&quot;&gt;hello&lt;/span&gt;&lt;/div&gt;&apos;</span>;
        <span class="hljs-keyword">let</span> container = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;container&apos;</span>);
        expect(container.nodeName.toLocaleLowerCase()).toBe(<span class="hljs-string">&apos;div&apos;</span>);
        <span class="hljs-keyword">let</span> hello = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;hello&apos;</span>);
        expect(hello.nodeName.toLocaleLowerCase()).toBe(<span class="hljs-string">&apos;span&apos;</span>);
        remove(hello);
        <span class="hljs-keyword">let</span> hello2 = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;hello&apos;</span>);
        expect(hello2).toBeNull();
    })

    test(<span class="hljs-string">&apos;on&apos;</span>, () =&gt; {
        <span class="hljs-built_in">document</span>.body.innerHTML = <span class="hljs-string">&apos;&lt;div id=&quot;container&quot;&gt;&lt;button id=&quot;clickMe&quot;&gt;click&lt;/button&gt;&lt;/div&gt;&apos;</span>;
        <span class="hljs-keyword">let</span> clickMe = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;clickMe&apos;</span>);
        on(clickMe, <span class="hljs-string">&apos;click&apos;</span>, () =&gt; {
            clickMe.innerHTML = <span class="hljs-string">&apos;clicked&apos;</span>;
        });
        clickMe.click();
        expect(clickMe.innerHTML).toBe(<span class="hljs-string">&apos;clicked&apos;</span>);

    })
});
</code></pre>
<h3 id="t186.7 &#x6D4B;&#x8BD5;&#x9009;&#x9879;&#x5361;">6.7 &#x6D4B;&#x8BD5;&#x9009;&#x9879;&#x5361; <a href="#t186.7 &#x6D4B;&#x8BD5;&#x9009;&#x9879;&#x5361;"> # </a></h3>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;tab&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;tab-button&quot;</span>&gt;</span>&#x9009;&#x9879;1<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;#&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;tab-button&quot;</span>&gt;</span>&#x9009;&#x9879;2<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;tab-panel&quot;</span>&gt;</span>&#x9762;&#x677F;1<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;tab-panel&quot;</span>&gt;</span>&#x9762;&#x677F;2<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<pre><code class="lang-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Tab</span></span>{
    <span class="hljs-keyword">constructor</span>(id,buttonClass,panelClass){
        <span class="hljs-keyword">this</span>.tab = tab = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#&apos;</span>+id);
        <span class="hljs-keyword">this</span>.buttons = <span class="hljs-built_in">Array</span>.from(tab.querySelectorAll(<span class="hljs-string">&apos;.&apos;</span>+buttonClass));
        <span class="hljs-keyword">this</span>.panels = <span class="hljs-built_in">Array</span>.from(tab.querySelectorAll(<span class="hljs-string">&apos;.&apos;</span>+panelClass));
        <span class="hljs-keyword">this</span>.select(<span class="hljs-number">0</span>);
        <span class="hljs-keyword">this</span>.bindEvent();
    }
    select(index){
        <span class="hljs-keyword">this</span>.buttons.forEach(<span class="hljs-function"><span class="hljs-params">button</span>=&gt;</span>button.style.backgroundColor= <span class="hljs-string">&apos;white&apos;</span>);
        <span class="hljs-keyword">this</span>.buttons[index].style.backgroundColor= <span class="hljs-string">&apos;red&apos;</span>;
        <span class="hljs-keyword">this</span>.panels.forEach(<span class="hljs-function"><span class="hljs-params">panel</span>=&gt;</span>panel.style.display= <span class="hljs-string">&apos;none&apos;</span>);
        <span class="hljs-keyword">this</span>.panels[index].style.display= <span class="hljs-string">&apos;block&apos;</span>;
    }
    bindEvent(){
        <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-keyword">this</span>.buttons.length;i++){
            <span class="hljs-keyword">this</span>.buttons[i].addEventListener(<span class="hljs-string">&apos;click&apos;</span>,()=&gt;{
                <span class="hljs-keyword">this</span>.select(i);
            });
        }
    }
}
<span class="hljs-built_in">module</span>.exports = Tab;
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Tab = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../src/tab&apos;</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);

test(<span class="hljs-string">&apos;tab&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">document</span>.body.innerHTML = fs.readFileSync(path.resolve(__dirname,<span class="hljs-string">&apos;tab.html&apos;</span>),<span class="hljs-string">&apos;utf8&apos;</span>);
    <span class="hljs-keyword">const</span> tab = <span class="hljs-keyword">new</span> Tab(<span class="hljs-string">&apos;tab&apos;</span>,<span class="hljs-string">&apos;tab-button&apos;</span>,<span class="hljs-string">&apos;tab-panel&apos;</span>);
    expect(tab.buttons[<span class="hljs-number">0</span>].style.backgroundColor).toBe(<span class="hljs-string">&apos;red&apos;</span>);
    expect(tab.buttons[<span class="hljs-number">1</span>].style.backgroundColor).toBe(<span class="hljs-string">&apos;white&apos;</span>);
    expect(tab.panels[<span class="hljs-number">0</span>].style.display).toBe(<span class="hljs-string">&apos;block&apos;</span>);
    expect(tab.panels[<span class="hljs-number">1</span>].style.display).toBe(<span class="hljs-string">&apos;none&apos;</span>);
    tab.buttons[<span class="hljs-number">1</span>].click();
    expect(tab.buttons[<span class="hljs-number">0</span>].style.backgroundColor).toBe(<span class="hljs-string">&apos;white&apos;</span>);
    expect(tab.buttons[<span class="hljs-number">1</span>].style.backgroundColor).toBe(<span class="hljs-string">&apos;red&apos;</span>);
    expect(tab.panels[<span class="hljs-number">0</span>].style.display).toBe(<span class="hljs-string">&apos;none&apos;</span>);
    expect(tab.panels[<span class="hljs-number">1</span>].style.display).toBe(<span class="hljs-string">&apos;block&apos;</span>);
});
</code></pre>
<h3 id="t196.8 &#x5F02;&#x6B65;&#x6D4B;&#x8BD5;">6.8 &#x5F02;&#x6B65;&#x6D4B;&#x8BD5; <a href="#t196.8 &#x5F02;&#x6B65;&#x6D4B;&#x8BD5;"> # </a></h3>
<h4 id="t206.8.1 &#x666E;&#x901A;&#x5F02;&#x6B65;">6.8.1 &#x666E;&#x901A;&#x5F02;&#x6B65; <a href="#t206.8.1 &#x666E;&#x901A;&#x5F02;&#x6B65;"> # </a></h4>
<pre><code class="lang-js">test(<span class="hljs-string">&apos;async&apos;</span>,(done)=&gt;{
    setTimeout(<span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{
        expect(<span class="hljs-number">2</span>).toBe(<span class="hljs-number">2</span>);
        done();
    },<span class="hljs-number">1000</span>);
});
</code></pre>
<h4 id="t216.8.2 ajax&#x5F02;&#x6B65;">6.8.2 ajax&#x5F02;&#x6B65; <a href="#t216.8.2 ajax&#x5F02;&#x6B65;"> # </a></h4>
<ul>
<li>Mocks&#x53EF;&#x4EE5;&#x64E6;&#x9664;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x9645;&#x5B9E;&#x73B0;&#x6765;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#x4E4B;&#x95F4;&#x7684;&#x94FE;&#x63A5;</li>
<li>Mocks&#x53EF;&#x4EE5;&#x6355;&#x83B7;&#x5BF9;&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;</li>
<li>manual_mocks&#x7528;&#x53EF;mock&#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x653E;&#x7F6E;&#x5728;&#x76F8;&#x5E94;<strong>mocks</strong>&#x76EE;&#x5F55;&#x4E0B;</li>
<li>&#x4F7F;&#x7528;mock function&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#x6B21;&#x6570;&#xFF0C;&#x4EE5;&#x53CA;&#x53C2;&#x6570;</li>
</ul>
<p>__tests__\users.js</p>
<pre><code class="lang-js">jest.mock(<span class="hljs-string">&apos;../js/ajax&apos;</span>);
<span class="hljs-keyword">const</span> getUsers = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../js/getUsers&apos;</span>);
test(<span class="hljs-string">&apos;getUsers&apos;</span>,(done)=&gt;{
    <span class="hljs-built_in">document</span>.body.innerHTML = <span class="hljs-string">`&lt;ul id=&quot;users&quot;&gt;&lt;/ul&gt;`</span>;
    getUsers(<span class="hljs-string">&apos;/users.json&apos;</span>,<span class="hljs-string">&apos;users&apos;</span>,()=&gt;{
        <span class="hljs-keyword">const</span> ul = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#users&apos;</span>);
        <span class="hljs-keyword">const</span> lis = ul.querySelectorAll(<span class="hljs-string">&apos;li&apos;</span>);
        expect(lis.length).toBe(<span class="hljs-number">2</span>);
        expect(lis[<span class="hljs-number">1</span>].innerHTML).toBe(<span class="hljs-string">&apos;zfpx2&apos;</span>);
        done();
    });
});
</code></pre>
<p>\js\getUsers.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> ajax = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./ajax&apos;</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUsers</span>(<span class="hljs-params">url,id,callback</span>)</span>{
 <span class="hljs-keyword">const</span> users = <span class="hljs-built_in">document</span>.getElementById(id);
 ajax(url,data=&gt;{
     <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;ajax ok&apos;</span>);
     data = <span class="hljs-built_in">JSON</span>.parse(data);
     users.innerHTML = data.map(<span class="hljs-function"><span class="hljs-params">item</span>=&gt;</span><span class="hljs-string">`&lt;li&gt;<span class="hljs-subst">${item.name}</span>&lt;/li&gt;`</span>).join(<span class="hljs-string">&apos;&apos;</span>);
     callback&amp;&amp;callback();
 });
}

<span class="hljs-built_in">module</span>.exports = getUsers;
</code></pre>
<p>\js\ajax.js</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ajax</span>(<span class="hljs-params">url,success</span>)</span>{
  <span class="hljs-keyword">const</span> xhr = <span class="hljs-keyword">new</span> XMLHttpRequest();
  xhr.onreadystatechange = <span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{
      <span class="hljs-keyword">if</span>(xhr.readyState == <span class="hljs-number">4</span>){
          success&amp;success(xhr.responseText);
      }
  }
  xhr.open(<span class="hljs-string">&apos;get&apos;</span>,url);
  xhr.send(<span class="hljs-literal">null</span>);
}
<span class="hljs-built_in">module</span>.exports = ajax;
</code></pre>
<p>js__mocks__\ajax.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> ajax = <span class="hljs-function">(<span class="hljs-params">url,success</span>)=&gt;</span>{
   setTimeout(<span class="hljs-function"><span class="hljs-params">()</span>=&gt;</span>{
       success(<span class="hljs-built_in">JSON</span>.stringify([{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx1&apos;</span>},{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>}]));
   },<span class="hljs-number">1000</span>);
}
<span class="hljs-built_in">module</span>.exports = ajax;
</code></pre>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;viewport&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">&quot;X-UA-Compatible&quot;</span> <span class="hljs-attr">content</span>=<span class="hljs-string">&quot;ie=edge&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;users&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
        <span class="hljs-keyword">let</span> <span class="hljs-built_in">module</span> = {};
        <span class="hljs-keyword">let</span> <span class="hljs-built_in">require</span> = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> ajax;
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;/js/ajax.js&quot;</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;/js/getUsers.js&quot;</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="actionscript">
        getUsers(<span class="hljs-string">&apos;http://localhost:3000/users.json&apos;</span>, <span class="hljs-string">&apos;users&apos;</span>);
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>users.html</p>
<pre><code class="lang-js">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;ul id=&quot;users&quot;&gt;&lt;/ul&gt;
    &lt;script&gt;
        let module = {};
        let require = () =&gt; ajax;
    &lt;/script&gt;
    &lt;script src=&quot;/js/ajax.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;/js/getUsers.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;
        getUsers(&apos;http://localhost:3000/users.json&apos;, &apos;users&apos;);
    &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>server.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> app = express();
app.use(express.static(__dirname));
app.get(<span class="hljs-string">&apos;/&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
    res.sendFile(path.resolve(__dirname,<span class="hljs-string">&apos;users.html&apos;</span>));
});

app.get(<span class="hljs-string">&apos;/users.json&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">req,res</span>)</span>{
  res.json([{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx1&apos;</span>},{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;zfpx2&apos;</span>}]);
});
app.listen(<span class="hljs-number">3000</span>);
</code></pre>
<h4 id="t226.9 &#x4EE3;&#x7801;&#x8986;&#x76D6;&#x7387;">6.9 &#x4EE3;&#x7801;&#x8986;&#x76D6;&#x7387; <a href="#t226.9 &#x4EE3;&#x7801;&#x8986;&#x76D6;&#x7387;"> # </a></h4>
<ul>
<li>line coverage &#x884C;&#x8986;&#x76D6;&#x7387;</li>
<li>function coverage &#x51FD;&#x6570;&#x8986;&#x76D6;&#x7387;</li>
<li>branch coverage &#x5206;&#x652F;&#x8986;&#x76D6;&#x7387;</li>
<li>statement coverage &#x8BED;&#x53E5;&#x8986;&#x76D6;&#x7387;</li>
</ul>
<pre><code class="lang-js">npx jest --coverage
</code></pre>
<h2 id="t237.&#x9644;&#x5F55;">7.&#x9644;&#x5F55; <a href="#t237.&#x9644;&#x5F55;"> # </a></h2>
<h3 id="t247.1 glob">7.1 glob <a href="#t247.1 glob"> # </a></h3>
<p>gulp&#x5185;&#x90E8;&#x4F7F;&#x7528;&#x4E86;node-glob&#x6A21;&#x5757;&#x6765;&#x5B9E;&#x73B0;&#x5176;&#x6587;&#x4EF6;&#x5339;&#x914D;&#x529F;&#x80FD;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x8FD9;&#x4E9B;&#x7279;&#x6B8A;&#x7684;&#x5B57;&#x7B26;&#x6765;&#x5339;&#x914D;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x7684;&#x6587;&#x4EF6;&#xFF1A;</p>
<h4 id="t257.2 glob&#x89C4;&#x5219;">7.2 glob&#x89C4;&#x5219; <a href="#t257.2 glob&#x89C4;&#x5219;"> # </a></h4>
<table>
<thead>
<tr>
<th style="text-align:left">&#x5339;&#x914D;&#x7B26;</th>
<th style="text-align:left">&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">&#x661F;</td>
<td style="text-align:left">&#x5339;&#x914D;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;&#x4E2D;&#x7684;0&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x4F46;&#x4E0D;&#x4F1A;&#x5339;&#x914D;&#x8DEF;&#x5F84;&#x5206;&#x9694;&#x7B26;</td>
</tr>
<tr>
<td style="text-align:left">**</td>
<td style="text-align:left">&#x5339;&#x914D;&#x8DEF;&#x5F84;&#x4E2D;&#x7684;0&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x76EE;&#x5F55;&#x53CA;&#x5176;&#x5B50;&#x76EE;&#x5F55;</td>
</tr>
<tr>
<td style="text-align:left">[...]</td>
<td style="text-align:left">&#x5339;&#x914D;&#x65B9;&#x62EC;&#x53F7;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x5B57;&#x7B26;&#x4E2D;&#x7684;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#xFF0C;&#x5F53;&#x65B9;&#x62EC;&#x53F7;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E3A;^&#x6216;!&#x65F6;&#xFF0C;&#x5219;&#x8868;&#x793A;&#x4E0D;&#x5339;&#x914D;&#x65B9;&#x62EC;&#x53F7;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x5176;&#x4ED6;&#x5B57;&#x7B26;&#x4E2D;&#x7684;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;</td>
</tr>
<tr>
<td style="text-align:left">!(pattern pattern pattern)</td>
<td style="text-align:left">&#x5339;&#x914D;&#x4EFB;&#x4F55;&#x4E0E;&#x62EC;&#x53F7;&#x4E2D;&#x7ED9;&#x5B9A;&#x7684;&#x4EFB;&#x4E00;&#x6A21;&#x5F0F;&#x90FD;&#x4E0D;&#x5339;&#x914D;&#x7684;</td>
</tr>
<tr>
<td style="text-align:left">?(pattern pattern pattern)</td>
<td style="text-align:left">&#x5339;&#x914D;&#x62EC;&#x53F7;&#x4E2D;&#x7ED9;&#x5B9A;&#x7684;&#x4EFB;&#x4E00;&#x6A21;&#x5F0F;0&#x6B21;&#x6216;1&#x6B21;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;js&#x6B63;&#x5219;&#x4E2D;&#x7684;?</td>
</tr>
<tr>
<td style="text-align:left">+(pattern pattern pattern)</td>
<td style="text-align:left">&#x5339;&#x914D;&#x62EC;&#x53F7;&#x4E2D;&#x7ED9;&#x5B9A;&#x7684;&#x4EFB;&#x4E00;&#x6A21;&#x5F0F;&#x81F3;&#x5C11;1&#x6B21;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;js&#x6B63;&#x5219;&#x4E2D;&#x7684;+</td>
</tr>
<tr>
<td style="text-align:left">(pattern pattern pattern)</td>
<td style="text-align:left">&#x5339;&#x914D;&#x62EC;&#x53F7;&#x4E2D;&#x7ED9;&#x5B9A;&#x7684;&#x4EFB;&#x4E00;&#x6A21;&#x5F0F;0&#x6B21;&#x6216;&#x591A;&#x6B21;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;js&#x6B63;&#x5219;&#x4E2D;&#x7684; *</td>
</tr>
<tr>
<td style="text-align:left">@(pattern pattern pattern)</td>
<td style="text-align:left">&#x5339;&#x914D;&#x62EC;&#x53F7;&#x4E2D;&#x7ED9;&#x5B9A;&#x7684;&#x4EFB;&#x4E00;&#x6A21;&#x5F0F;1&#x6B21;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;js&#x6B63;&#x5219;&#x4E2D;&#x7684;</td>
</tr>
</tbody>
</table>
<h4 id="t267.3 glob&#x793A;&#x4F8B;">7.3 glob&#x793A;&#x4F8B; <a href="#t267.3 glob&#x793A;&#x4F8B;"> # </a></h4>
<table>
<thead>
<tr>
<th style="text-align:left">glob</th>
<th style="text-align:left">&#x5339;&#x914D;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">*</td>
<td style="text-align:left">&#x80FD;&#x5339;&#x914D; a.js,x.y,abc,abc/,&#x4F46;&#x4E0D;&#x80FD;&#x5339;&#x914D;a/b.js</td>
<td></td>
</tr>
<tr>
<td style="text-align:left"><em>.</em></td>
<td style="text-align:left">a.js,style.css,a.b,x.y</td>
</tr>
<tr>
<td style="text-align:left"><em>/</em>/*.js</td>
<td style="text-align:left">&#x80FD;&#x5339;&#x914D; a/b/c.js,x/y/z.js,&#x4E0D;&#x80FD;&#x5339;&#x914D;a/b.js,a/b/c/d.js</td>
</tr>
<tr>
<td style="text-align:left">**</td>
<td style="text-align:left">&#x80FD;&#x5339;&#x914D; abc,a/b.js,a/b/c.js,x/y/z,x/y/z/a.b,&#x80FD;&#x7528;&#x6765;&#x5339;&#x914D;&#x6240;&#x6709;&#x7684;&#x76EE;&#x5F55;&#x548C;&#x6587;&#x4EF6;</td>
</tr>
<tr>
<td style="text-align:left">a/**/z</td>
<td style="text-align:left">&#x80FD;&#x5339;&#x914D; a/z,a/b/z,a/b/c/z,a/d/g/h/j/k/z</td>
</tr>
<tr>
<td style="text-align:left">a/**b/z</td>
<td style="text-align:left">&#x80FD;&#x5339;&#x914D; a/b/z,a/sb/z,&#x4F46;&#x4E0D;&#x80FD;&#x5339;&#x914D;a/x/sb/z,&#x56E0;&#x4E3A;&#x53EA;&#x6709;&#x5355;**&#x5355;&#x72EC;&#x51FA;&#x73B0;&#x624D;&#x80FD;&#x5339;&#x914D;&#x591A;&#x7EA7;&#x76EE;&#x5F55;</td>
</tr>
<tr>
<td style="text-align:left">?.js</td>
<td style="text-align:left">&#x80FD;&#x5339;&#x914D; a.js,b.js,c.js</td>
</tr>
<tr>
<td style="text-align:left">a??</td>
<td style="text-align:left">&#x80FD;&#x5339;&#x914D; a.b,abc,&#x4F46;&#x4E0D;&#x80FD;&#x5339;&#x914D;ab/,&#x56E0;&#x4E3A;&#x5B83;&#x4E0D;&#x4F1A;&#x5339;&#x914D;&#x8DEF;&#x5F84;&#x5206;&#x9694;&#x7B26;</td>
</tr>
<tr>
<td style="text-align:left">[xyz].js</td>
<td style="text-align:left">&#x53EA;&#x80FD;&#x5339;&#x914D; x.js,y.js,z.js,&#x4E0D;&#x4F1A;&#x5339;&#x914D;xy.js,xyz.js&#x7B49;,&#x6574;&#x4E2A;&#x4E2D;&#x62EC;&#x53F7;&#x53EA;&#x4EE3;&#x8868;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">[^xyz].js</td>
<td style="text-align:left">&#x80FD;&#x5339;&#x914D; a.js,b.js,c.js&#x7B49;,&#x4E0D;&#x80FD;&#x5339;&#x914D;x.js,y.js,z.js</td>
</tr>
</tbody>
</table>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
